const Service = require('egg').Service;
class HomeController extends Service {
  //查询用户
  async checkUser(obj){
    try{
      const userList = await this.app.mysql.query('select * from user where username =?',obj.username);
      return {
        code:1,
        isExists:userList.length>0
      }
    }catch(err){
      return {
        code:0,
        msg:err.message,
      };
    }
  }
  //添加用户
  async reigster(user){
    try{
      await this.app.mysql.insert('user',user)
      return {
        code: 1,
        msg: '注册成功'
      }
    } catch(e) {
      return {
        code: 0,
        msg: e.message
      }
    }
  }
  //
  async isLogin(obj){
    try{
      const userList = await this.app.mysql.query('select * from user where username =? and password =?',[obj.username,obj.password])
      return {
        code:1,
        isExists:userList.length>0
      }
    }catch(err){
      return {
        code:0,
        msg:err.message
      };
    }
  }
}

module.exports = HomeController;
